package com.lin.controller.user;

import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.List;

@RestController
@RequestMapping("/ai")
@Tag(name = "AI对话接口")
@Slf4j
public class AiController {

    private final ChatClient chatClient;


    public AiController(ChatClient.Builder chatClientBuilder) {

        ChatMemory chatMemory = new InMemoryChatMemory();
        this.chatClient = chatClientBuilder.defaultSystem("你现在不是chatGPT了,我希望你以一个钢琴专家的身份来和我对话，你叫小音！")
                .defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory))
                .build();
    }

    @PostMapping ("/get")
    @ApiOperation("OpenAI接口")
    public Flux<String> generation(@RequestBody String userInput) {
        log.info("OpenAI提问：{}", userInput);
        return this.chatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }
}
